import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DateFormat;
import java.util.Calendar;
import java.util.Date;

import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import javax.swing.border.EmptyBorder;


public class PhoneLayout extends JFrame{

	/* Screen is 480 x 800 */

	public CalendarEx cal; // calendar (using given code)

	public CalendarDate date;

	public JLabel dateLabel;

	public PhoneMonthLayout monthView; //THIS ALLOWS ME TO UPDATE THE TOP TEXT

	public AgendaView agendaView; // THIS IS NEEDED TO ALLOW UPDATING OF TEXT AT TOP
	//OF SCREEN. WILL EXPLAIN LATER

	public PhoneWeekLayout weekView;
	
	public EditEvent editScreen;

	public int[] monthValues = new int[] {0,31,29,31,30,31,30,31,31,30,31,30,31};

	public PhoneDayLayout dayView;

	public JPanel navigationPanel;

	public JPanel screenPanel; // so can acccess it and update it from other classes...

	public JPanel mainScreenP; // WHite part in middle of screen. Usually want to change this!

	public JPanel cards; // FOR CARD LAYOUT for whole screen

	public JPanel middleCards; // used for agenda, day, week and month views. Means when coming out of settings/add event we will be retuend back to the view we were on

	//public JPanel topBar; // inc the date in top left and agenda,day,week,month view buttons

	public JPanel botBar; // bottom buttons public so can use it on other screens

	public Date topLeftDate; // so can change from different views

	public String dateString; // need to change this as well as date

	public String monthStr;

	private JButton todayBut, addBut, searchBut, filterBut, settingsBut; // bottom buttons instance variables for event handling

	public JButton swipeLeftBut, swipeRightBut; // so can disbale them on screens

	private JButton agendaBut, dayBut, weekBut, monthBut; // top bar buttons

	public AddEvent addEventScreen ;// so every screen knows of the add event screen (so can pass date and time etc)

	public Date today; public Calendar c;

	//Date topLeftDate; // date in top left
	//JLabel dateLabel;

	public PhoneLayout(){

		cal = new CalendarEx();

		cal.openCalendar("./calendar.txt");

		cards = new JPanel(new CardLayout()); //MAIN WHOLE PHONE SCREEN PART



        // new Date() is deprecated and doesn't work. It returns the year 111 instead of 2011. Use Calendar class instead.
        Calendar c = Calendar.getInstance();



		today = new Date();

                // new Date() is deprecated and doesn't work. It returns the year 111 instead of 2011. Use Calendar class instead.
        c = Calendar.getInstance();

		date = new CalendarDate( c.get(Calendar.DAY_OF_MONTH), c.get(Calendar.MONTH)+1, c.get(Calendar.YEAR) );

		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


		/* build full screen */
		screenPanel = new JPanel();
		screenPanel.setLayout(new BoxLayout(screenPanel,BoxLayout.Y_AXIS));
		screenPanel.setBackground(Color.white);
		screenPanel.setMaximumSize(new Dimension(480,800));
		screenPanel.setMinimumSize(new Dimension(480,800));
		screenPanel.setPreferredSize(screenPanel.getMaximumSize());

		cards.add(screenPanel, "home");

		/* build top navigation bar*/
		navigationPanel = new JPanel(new BorderLayout());
		navigationPanel.setBackground(Color.darkGray);
		navigationPanel.setPreferredSize(new Dimension(480,80));
		screenPanel.add(navigationPanel);

		/* create and add date to top bar*/
		topLeftDate = new Date();
		dateString =  "   " + DateFormat.getDateInstance().format(topLeftDate);
		dateLabel = new JLabel(dateString);
		dateLabel.setForeground(Color.white);
		dateLabel.setFont(new Font("Arial",Font.PLAIN,25));
		navigationPanel.add(BorderLayout.WEST,dateLabel);

		/* add view button to top bar (agenda, day, week, month) */

		TopButtonsActionListener topListener = new TopButtonsActionListener();

		JPanel buttonPanel = new JPanel();
		buttonPanel.setLayout(new BoxLayout(buttonPanel,BoxLayout.X_AXIS));
		buttonPanel.setBackground(Color.darkGray);
		buttonPanel.setPreferredSize(new Dimension(220, 80));
		agendaBut = new JButton("Agenda");
		agendaBut.setPreferredSize(new Dimension(50,40));
		agendaBut.addActionListener(topListener);
		buttonPanel.add(agendaBut);
		dayBut = new JButton("Day");
		dayBut.setPreferredSize(new Dimension(50,40));
		dayBut.addActionListener(topListener);
		buttonPanel.add(dayBut);
		weekBut = new JButton("Week");
		weekBut.setPreferredSize(new Dimension(50,40));
		weekBut.addActionListener(topListener);
		buttonPanel.add(weekBut);
		monthBut = new JButton("Month");
		monthBut.setPreferredSize(new Dimension(50,40));
		monthBut.addActionListener(topListener);
		buttonPanel.add(monthBut);

		navigationPanel.add(BorderLayout.EAST,buttonPanel);

		/* build main screen THE MIDDLE WHITE BIT*/
		/* this is now a set of cards so agenda,day,week and month can easily be switched too */
		middleCards = new JPanel(new CardLayout()); // WHITE CENTER PART OF SCREEN
		middleCards.setBackground(Color.white);
		middleCards.setPreferredSize(new Dimension(480,640));

		screenPanel.add(middleCards);

		/* build bottom navigation bar*/
		botBar = new JPanel(new GridLayout(1,5,4,4));
		botBar.setBorder(new EmptyBorder(4,4,4,4));
		botBar.setBackground(Color.DARK_GRAY);
		botBar.setPreferredSize(new Dimension(480,80));
		screenPanel.add(botBar);

		BottomButtonsActionListener bottomListener = new BottomButtonsActionListener();

		todayBut = new JButton("Today");
		todayBut.addActionListener(bottomListener);
	    todayBut.setVerticalTextPosition(SwingConstants.BOTTOM);
	    todayBut.setHorizontalTextPosition(SwingConstants.CENTER);
		ImageIcon todayPic = new ImageIcon("today.png");
		Image img = todayPic.getImage() ;  
		Image newimg = img.getScaledInstance( 42, 42,  java.awt.Image.SCALE_SMOOTH ) ;  
		todayPic = new ImageIcon( newimg );
		
		todayBut.setIcon(todayPic);
		botBar.add(todayBut);

		addBut= new JButton("Add Event");
		addBut.addActionListener(bottomListener);
	    addBut.setVerticalTextPosition(SwingConstants.BOTTOM);
	    addBut.setHorizontalTextPosition(SwingConstants.CENTER);


		ImageIcon addPic = new ImageIcon("add.png");
		Image img2 = addPic.getImage() ;  
		Image newimg2 = img2.getScaledInstance( 42, 42,  java.awt.Image.SCALE_SMOOTH ) ;  
		addPic = new ImageIcon( newimg2 );
		
		addBut.setIcon(addPic);
		botBar.add(addBut);

		searchBut = new JButton("Search");
		searchBut.addActionListener(bottomListener);
	    searchBut.setVerticalTextPosition(SwingConstants.BOTTOM);
	    searchBut.setHorizontalTextPosition(SwingConstants.CENTER);
		ImageIcon searchPic = new ImageIcon("search.png");
		Image img3 = searchPic.getImage() ;  
		Image newimg3 = img3.getScaledInstance( 42, 42,  java.awt.Image.SCALE_SMOOTH ) ;  
		searchPic = new ImageIcon( newimg3 );
		
		searchBut.setIcon(searchPic);
		botBar.add(searchBut);

		filterBut = new JButton("Filter");
		filterBut.addActionListener(bottomListener);
	    filterBut.setVerticalTextPosition(SwingConstants.BOTTOM);
	    filterBut.setHorizontalTextPosition(SwingConstants.CENTER);
		ImageIcon filterPic = new ImageIcon("filter.png");
		Image img4 = filterPic.getImage() ;  
		Image newimg4 = img4.getScaledInstance( 42, 42,  java.awt.Image.SCALE_SMOOTH ) ;  
		filterPic = new ImageIcon( newimg4 );
		
		filterBut.setIcon(filterPic);
		botBar.add(filterBut);

		settingsBut = new JButton("Options");
		settingsBut.addActionListener(bottomListener);
	    settingsBut.setVerticalTextPosition(SwingConstants.BOTTOM);
	    settingsBut.setHorizontalTextPosition(SwingConstants.CENTER);
		ImageIcon settingsPic = new ImageIcon("settings.png");
		Image img5 = settingsPic.getImage() ;  
		Image newimg5 = img5.getScaledInstance( 42, 42,  java.awt.Image.SCALE_SMOOTH ) ;  
		settingsPic = new ImageIcon( newimg5 );
		
		settingsBut.setIcon(settingsPic);
		botBar.add(settingsBut);



		// Put the stuff on screen
            //    SwipeButtonActionListener swipeAL = new SwipeButtonActionListener();
		Container pan = this.getContentPane();
		pan.setLayout(new BoxLayout(pan, BoxLayout.X_AXIS));

		swipeLeftBut = new JButton("Swipe Left");
         //       swipeLeftBut.addActionListener( swipeAL );
		pan.add(swipeLeftBut);



		pan.add(cards);


		swipeRightBut = new JButton("Swipe Right");
          //      swipeRightBut.addActionListener( swipeAL );
		pan.add(swipeRightBut);


		pack();
		setVisible(true);


		/** Create other screens below */

		addEventScreen = new AddEvent(this); // add add event screen to app screen

		Settings settings = new Settings(this);

		Search search = new Search(this);

		agendaView = new AgendaView(this);

		monthView = new PhoneMonthLayout(this);

		weekView = new PhoneWeekLayout(this);

		dayView = new PhoneDayLayout(this);
		
		editScreen = new EditEvent(this);

                CardLayout cl = (CardLayout) middleCards.getLayout();
                cl.show(middleCards, "Month");
		/*
		 * THIS IS PAULS CODE - NEEDED TO CHANGE THE MONTH ON THE TOP LEFT
		 */

		Calendar current = Calendar.getInstance();

		int currentD = current.get(Calendar.DAY_OF_MONTH);
		int currentM = current.get(Calendar.MONTH) +1; 		// jan is randomly 0..
		int currentY = current.get(Calendar.YEAR);
		int month = currentM;
		monthStr = "";

		switch(month) {
		case 1:
			monthStr = "January";
			monthView.updateTopText(monthStr);
			break;
		case 2:
			monthStr = "February";
			monthView.updateTopText(monthStr);
			break;
		case 3:
			monthStr = "March";
			monthView.updateTopText(monthStr);
			break;
		case 4:
			monthStr = "April";
			monthView.updateTopText(monthStr);
			break;
		case 5:
			monthStr = "May";
			monthView.updateTopText(monthStr);
			break;
		case 6:
			monthStr = "June";
			monthView.updateTopText(monthStr);
			break;
		case 7:
			monthStr = "July";
			monthView.updateTopText(monthStr);
			break;
		case 8:
			monthStr = "August";
			monthView.updateTopText(monthStr);
			break;
		case 9:
			monthStr = "September";
			monthView.updateTopText(monthStr);
			break;
		case 10:
			monthStr = "October";
			monthView.updateTopText(monthStr);
			break;
		case 11:
			monthStr = "November";
			monthView.updateTopText(monthStr);
			break;
		case 12:
			monthStr = "December";
			monthView.updateTopText(monthStr);
			break;
		}
		


	}
	
	public String getDateString (int month){
		switch(month) {
		case 1:
			monthStr = "January";
			monthView.updateTopText(monthStr);
			return monthStr;
		case 2:
			monthStr = "February";
			monthView.updateTopText(monthStr);
			return monthStr;
		case 3:
			monthStr = "March";
			monthView.updateTopText(monthStr);
			return monthStr;
		case 4:
			monthStr = "April";
			monthView.updateTopText(monthStr);
			return monthStr;

		case 5:
			monthStr = "May";
			monthView.updateTopText(monthStr);
			return monthStr;
		case 6:
			monthStr = "June";
			monthView.updateTopText(monthStr);
			return monthStr;
		case 7:
			monthStr = "July";
			monthView.updateTopText(monthStr);
			return monthStr;
		case 8:
			monthStr = "August";
			monthView.updateTopText(monthStr);
			return monthStr;
		case 9:
			monthStr = "September";
			monthView.updateTopText(monthStr);
			return monthStr;
		case 10:
			monthStr = "October";
			monthView.updateTopText(monthStr);
			return monthStr;
		case 11:
			monthStr = "November";
			monthView.updateTopText(monthStr);
			return monthStr;
		case 12:
			monthStr = "December";
			monthView.updateTopText(monthStr);
			return monthStr;
	}
		return "January";
	}

	/**
	 * @return the today
	 */
	public Date getToday() {
		return today;
	}


	/**
	 * @param today the today to set
	 */
	public void setToday(Date today) {
		this.today = today;
	}


	/**
	 * @return the cal
	 */
	public CalendarEx getCal() {
		return cal;
	}

	/**
	 * @param cal the cal to set
	 */
	public void setCal(CalendarEx cal) {
		this.cal = cal;
	}

	/**
	 * @return the dateString
	 */
	public String getDateString() {
		return dateString;
	}

	/**
	 * @param dateString the dateString to set
	 */
	public void setDateString(String dateString) {
		this.dateString = dateString;
	}

	/**
	 * @return the topLeftDate
	 */
	public Date getTopLeftDate() {
		return topLeftDate;
	}

	public void setTopText(String s) {
		dateLabel.setText("   "+s);
	}

	/* crap code but works */
        public void updateDayView() {
                dayView = new PhoneDayLayout(this);
        }
        
        public void updateWeekView() {
        	weekView = new PhoneWeekLayout(this);
        }

        public void updateAgendaView() {
            agendaView = new AgendaView(this);
    }
        
        public CalendarDate getTodaysDate() {
               Calendar c = Calendar.getInstance();
               return ( new CalendarDate( c.get(Calendar.DAY_OF_MONTH), c.get(Calendar.MONTH)+1, c.get(Calendar.YEAR) ) );
        }

        public void updateAll(){
        	updateDayView();
        	updateWeekView();
        	updateAgendaView();
        }
	class TopButtonsActionListener implements ActionListener{

		public TopButtonsActionListener(){
		}
		public void actionPerformed(ActionEvent ae){
			CardLayout cl = (CardLayout)(middleCards.getLayout());
			if (ae.getSource() == agendaBut){
				System.out.println("Agenda Button pressed.");
				setTopText("Agenda");
				updateAgendaView();
				cl.show(middleCards, "agenda");

				//agendaView.refreshAgenda();
			}
			else if (ae.getSource() == dayBut){
				System.out.println("Day Button pressed.");
                updateDayView();	
				cl.show(middleCards, "Day");
				swipeLeftBut.setEnabled(true);
				swipeRightBut.setEnabled(true);
			}
			else if (ae.getSource() == weekBut){
				System.out.println("Week Button pressed.");
				updateWeekView();
				cl.show(middleCards, "Week");
				setTopText(dateString.substring(3));
				swipeLeftBut.setEnabled(false);
				swipeRightBut.setEnabled(false);
			}
			else if (ae.getSource() == monthBut){
				System.out.println("Month Button pressed.");
				cl.show(middleCards, "Month");
				monthView.updateView();
				swipeLeftBut.setEnabled(true);
				swipeRightBut.setEnabled(true);
				/*
				 * The updateTopText method is different for my class as i need to pass in the month
				 * each time the month is changed - agenda wont need this.
				 * Day and Week view might.
				 */
				monthView.updateTopText(monthStr);

			}
		}

	}

	class BottomButtonsActionListener implements ActionListener{

		public BottomButtonsActionListener(){
		}
		public void actionPerformed(ActionEvent ae) {
			CardLayout cl = (CardLayout)(cards.getLayout());

			if(ae.getSource()==addBut){
				System.out.println("Add Button Pressed");
				cl.show(cards, "Add Event Panel");
				swipeLeftBut.setEnabled(false);
				swipeRightBut.setEnabled(false);
			}
			else if (ae.getSource()==todayBut) {
				System.out.println("Today Button Pressed");
                                CardLayout c = (CardLayout) middleCards.getLayout();
                                date = getTodaysDate();
                                updateDayView();
                                c.show(middleCards, "Day");
                        }
			else if (ae.getSource()==searchBut){
				System.out.println("Search Button Pressed");
				cl.show(cards, "search");
			}
			else if (ae.getSource()==filterBut)
				System.out.println("Filter Button Pressed. Not yet implemented..");
			else if (ae.getSource()==settingsBut){
				System.out.println("Settings Button Pressed");
				cl.show(cards, "settings");
			}
		}


	}

      /* class SwipeButtonActionListener implements ActionListener {

               public void actionPerformed( ActionEvent ae ) {

                      String[] views = new String[]{ "agenda", "Day", "Week", "Month" };
                      CardLayout cl = (CardLayout) middleCards.getLayout();
                      if ( ae.getSource() == swipeLeftBut ) {
                               cl.next(middleCards);
                      }
                      else if ( ae.getSource() == swipeRightBut ) {
                               cl.previous(middleCards);
                      }

              }
       }*/
}

